package com.sluggard.security.manage.controller;

import com.sluggard.common.model.res.Result;
import com.sluggard.mp.model.req.BaseReq;
import com.sluggard.mp.model.req.IdsReq;
import com.sluggard.mp.mvc.BaseController;
import com.sluggard.security.manage.model.entity.MenuEntity;
import com.sluggard.security.manage.model.entity.RoleEntity;
import com.sluggard.security.manage.model.entity.RoleMenuEntity;
import com.sluggard.security.manage.model.req.RoleOptionSearchReq;
import com.sluggard.security.manage.model.req.RoleReq;
import com.sluggard.security.manage.model.res.Option;
import com.sluggard.security.manage.service.IMenuService;
import com.sluggard.security.manage.service.IRoleMenuService;
import com.sluggard.security.manage.service.IRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色xinxi 前端控制器
 * </p>
 *
 * @author lizheng
 * @since 2024-04-28
 */
@Tag(name = "角色管理")
@RestController
@RequestMapping("/manage/system/role")
public class RoleController extends BaseController<RoleReq, RoleEntity, IRoleService> {

    @Resource
    private IRoleMenuService roleMenuService;

    @Resource
    private IMenuService menuService;

    @Override
    protected void afterSave(RoleReq req, RoleEntity instance) {
        roleMenuService.deleteByRoleId(req.getId());
        // 绑定角色菜单关联
        if (!req.getPermissionIds().isEmpty()) {
            roleMenuService.saveBatch(req.getPermissionIds()
                    .parallelStream()
                    .map(item -> {
                        RoleMenuEntity roleMenu = new RoleMenuEntity();
                        roleMenu.setMenuId(Long.parseLong(item));
                        roleMenu.setRoleId(req.getId());
                        return roleMenu;
                    })
                    .collect(Collectors.toSet()));
        }
    }

    @Override
    protected RoleEntity loadEntity(Long id) {
        RoleEntity roleEntity = super.loadEntity(id);
        List<Long> permissionIds = roleMenuService.selectByRoleId(roleEntity.getId());
        roleEntity.setPermissionIds(permissionIds.stream().map(Object::toString).collect(Collectors.toList()));
        return roleEntity;
    }

    @Operation(summary = "获取角色菜单")
    @PostMapping("getAllMenuNode")
    public Result<List<MenuEntity>> getAllMenuNode(@RequestBody BaseReq req) {
        if (Objects.isNull(req.getId()) || req.getId() < 0) {
            return Result.error("传入角色id异常");
        }
        return Result.ok(service.getMenuNodeByRoleId(req.getId()));
    }

    @Operation(summary = "角色列表")
    @PostMapping("options")
    public Result<List<Option>> roleList(@RequestBody RoleOptionSearchReq req) {
        return Result.ok(service.roleList(req.name()));
    }

    @Override
    protected void onDelete(IdsReq idsReq) {
        service.onDelete(idsReq);
    }
}
